package de.karlNet.gifSocket.presenter;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import org.apache.http.client.ClientProtocolException;

import de.karlNet.gifSocket.controllers.interfaces.ISessionController;

public class Presenter {
	private static String fileNames[];

	private static void readFiles(final String path) {
		// Directory path here
		ArrayList<String> listOfPngFiles = new ArrayList<String>();
		String files;
		File folder = new File(path);
		File[] listOfFiles = folder.listFiles();

		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				files = listOfFiles[i].getName();
				if (files.endsWith(".png")) {
					listOfPngFiles.add(files);
				}
			}
		}
		fileNames = listOfPngFiles.toArray(new String[listOfPngFiles.size()]);
	}

	/**
	 * @param args
	 * @throws AWTException
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static void main(String[] args) throws AWTException,
			ClientProtocolException, IOException {
		readFiles(args[0]);
		String hostName = args[1];
		ArrayList<BufferedImage> imagesToDeliver = new ArrayList<BufferedImage>();
		for (String fileName : fileNames) {
			BufferedImage img = null;
			try {
				img = ImageIO.read(new File(fileName));
				imagesToDeliver.add(img);
			} catch (IOException e) {
				e.printStackTrace();
				System.exit(1);
			}
		}

		final PresenterController sessionController = new PresenterController(
				hostName, imagesToDeliver);
		final Rectangle captureSize = new Rectangle(imagesToDeliver.get(0)
				.getWidth(), imagesToDeliver.get(0).getHeight());

		final Integer sessionNumber = Integer.parseInt(sessionController
				.createSession(captureSize.width, captureSize.height));
		System.out.println(sessionNumber);

		Thread clickReaderThread = new Thread(new Runnable() {

			public void run() {
				try {
					sessionController.addMouseListener(sessionNumber, null);
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		clickReaderThread.start();
	}

}
